﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace iHub
{
    public partial class createAccount : Form
    {
        private AccountManager acctm = new AccountManager();
        public createAccount()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Create_Closing);
        }

        private void acctcreateaccount_Click(object sender, EventArgs e)
        {
            Account naccount = new Account();
            naccount.setUsername(acctUsername.Text);
            naccount.setPass(acctPassword.Text);

            if (acctAdmin.Checked == true)
            {
                naccount.setUsertype(true);
            }
            else if (acctEncoder.Checked == true)
            {
                naccount.setUsertype(false);
            }

            naccount.setQuestion(acctsecurityquestion.Text);
            naccount.setAnswer(acctsecurityanswer.Text);
            naccount.setStatus(true);

            Boolean exists;
            DialogResult msg = MessageBox.Show("Create new account with selected details?", "Create new Account", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (msg == DialogResult.Yes)
            {
                if (acctUsername.Text != "" || acctPassword.Text != "" || (acctAdmin.Checked != false && acctEncoder.Checked != false) || acctsecurityanswer.Text != "" || acctsecurityquestion.Text != "")
                {
                    exists = acctm.userExists(naccount.getUsername());
                    if (!exists)
                    {
                        acctm.addUserAccount(naccount);
                        main.form.acctRefresh();
                        MessageBox.Show("Account Creation Successful", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Username already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                else
                {
                    MessageBox.Show("Please make sure that all fields are not empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Create_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                main.form.Enabled = true;
                main.form.acctRefresh();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult msg = MessageBox.Show("Are you sure you want to close this window?", "Close Window", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (msg == DialogResult.Yes)
            {
                main.form.Enabled = true;
                main.form.acctRefresh();
                this.Dispose();
            }
        }

        private void createAccount_Load(object sender, EventArgs e)
        {

        }
    }
}
